package com.ldc.commonlibs.uts

import android.app.Activity
import android.app.Application
import android.os.Bundle
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData


/**
 *@description
 *@author liudc
 *@data 2022/6/17 00:21
 *
 */
object ActivityUtils {
    private val activityList: MutableList<Activity> = mutableListOf()

    private val _topActivity: MutableLiveData<Activity> = MutableLiveData()
    private val topActivity: LiveData<Activity> = _topActivity

    fun getTopActivity(): Activity {
        return topActivity.value!!
    }

    fun finishAllActivity() {
        activityList.forEach { activity ->
            activity.finish()
        }
    }

    fun init(application: Application) {
        application.registerActivityLifecycleCallbacks(object :
            Application.ActivityLifecycleCallbacks {
            override fun onActivityCreated(p0: Activity, p1: Bundle?) {
                activityList.add(p0)
            }

            override fun onActivityStarted(p0: Activity) {
            }

            override fun onActivityResumed(p0: Activity) {
                _topActivity.value = p0
            }

            override fun onActivityPaused(p0: Activity) {

            }

            override fun onActivityStopped(p0: Activity) {

            }

            override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {

            }

            override fun onActivityDestroyed(p0: Activity) {
                activityList.remove(p0)
            }
        });
    }
}